package nail

import (
	"back/constant"
	"back/entity/nailEntity"
	"back/router/middle"
	requset "back/router/request"
	srv "back/service/model"
	"github.com/gin-gonic/gin"
	"utilx/rox"
)

type NailMember struct {
	srv srv.NailMember
}

func NewMember(srv srv.NailMember) NailMember {
	return NailMember{srv}
}
func (u NailMember) InitRouter(group *gin.RouterGroup) {
	user := group.Group("/member")
	//api/v1/
	//	xx/member
	//	api/v1/member/delById

	user.POST("/searchById", middle.CacheForce(), u.SearchById())
	user.POST("/searchBy", middle.CacheForce(), u.SearchBy())
	user.POST("/delById", u.DelById())
	user.POST("/updateById", u.UpdateById())
	user.POST("/updateBy", u.UpdateBy())
	user.POST("/create", u.Create())
	user.POST("/login", u.login()) // user.login
	//user.GET("/updateBy", u.UpdateBy())
}
func (u NailMember) SearchById() gin.HandlerFunc {
	return func(ctx *gin.Context) {
		var rec nailEntity.NailMember
		if rox.BindBoyJsonErr(ctx, &rec) {
			return
		}
		var data any
		var err error
		cacheForce := ctx.GetBool(constant.CacheForce)
		data, err = u.srv.CacheSearchById(cacheForce, rec.ID)
		if err != nil {
			rox.HandleErr[any](ctx, err.Error(), data)
			return
		}
		rox.HandleSuccess[any](ctx, "success", data)
	}
}
func (u NailMember) SearchBy() gin.HandlerFunc {
	return func(ctx *gin.Context) {
		var rec requset.SearchWithPage[nailEntity.NailMember]
		if rox.BindBoyJsonErr(ctx, &rec) {
			return
		}
		var data any
		var err error
		cacheForce := ctx.GetBool(constant.CacheForce)
		data, err = u.srv.CacheSearchBy(cacheForce, rec)

		if err != nil {
			rox.HandleErr[any](ctx, err.Error(), data)
			return
		}
		rox.HandleSuccess[any](ctx, "success", data)
	}
}
func (u NailMember) DelById() gin.HandlerFunc {
	return func(ctx *gin.Context) {

		var rec nailEntity.NailMember
		if rox.BindBoyJsonErr(ctx, &rec) {
			return
		}

		var data any
		var err error
		data, err = u.srv.DelById(rec.ID)
		if err != nil {
			rox.HandleErr[any](ctx, err.Error(), data)
			return
		}
		rox.HandleSuccess[any](ctx, "success", data)
	}
}
func (u NailMember) UpdateById() gin.HandlerFunc {
	return func(ctx *gin.Context) {
		var rec nailEntity.NailMember
		if rox.BindBoyJsonErr(ctx, &rec) {
			return
		}
		var data any
		var err error
		err = u.srv.UpdateById(rec)
		if err != nil {
			rox.HandleErr[any](ctx, err.Error(), data)
			return
		}
		rox.HandleSuccess[any](ctx, "success", data)
	}
}
func (u NailMember) UpdateBy() gin.HandlerFunc {
	return func(ctx *gin.Context) {
		var rec requset.UpdateBy[nailEntity.NailMember, nailEntity.NailMember]
		if rox.BindBoyJsonErr(ctx, &rec) {
			return
		}
		var data any
		var err error
		err = u.srv.UpdateBy(rec)
		if err != nil {
			rox.HandleErr[any](ctx, err.Error(), data)
			return
		}
		rox.HandleSuccess[any](ctx, "success", data)
	}
}
func (u NailMember) Create() gin.HandlerFunc {
	return func(ctx *gin.Context) {
		var rec nailEntity.NailMember
		if rox.BindBoyJsonErr(ctx, &rec) {
			return
		}
		var data any
		var err error
		err = u.srv.Create(rec)
		if err != nil {
			rox.HandleErr[any](ctx, err.Error(), data)
			return
		}
		rox.HandleSuccess[any](ctx, "success", data)
	}
}

// LOGIN
